package org.imsetal.ilchatservice.pojo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@Data
@TableName("dialogue")
public class Dialogue {
    @TableId(type = IdType.AUTO)
    Long id;
    Long userid;
    String messages;
    String createtime;

    public Dialogue addUserMessage(String content){
        JSONArray messages= JSON.parseArray(this.messages);
        JSONObject message=new JSONObject();
        message.put("role","user");
        message.put("content",content);
        if(messages!=null) {
            if (((JSONObject) messages.get(messages.size() - 1)).get("role").equals("user")) {
                System.out.println("对话角色异常");
                return this;
            }
        }else{
            messages=new JSONArray();
        }
        messages.add(message);
        this.messages=messages.toJSONString();
        return this;
    }

    public Dialogue addAssistantMessage(String content){
        JSONArray messages= JSON.parseArray(this.messages);
        JSONObject message=new JSONObject();
        message.put("role","assistant");
        message.put("content",content);
        if(messages!=null) {
            if (((JSONObject) messages.get(messages.size() - 1)).get("role").equals("assistant")) {
                System.out.println("对话角色异常");
                return this;
            }
        }else{
            messages=new JSONArray();
        }
        messages.add(message);
        this.messages=messages.toJSONString();
        return this;
    }
}
